﻿using CinemaMaestro.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace CinemaMaestro.WindowsApplication.EndPoints
{
    public class CinemaEndPoints
    {
        private const string CinemasUrl = "api/cinemas";
        private HttpClient client;

        public CinemaEndPoints(HttpClient client)
        {
            this.client = client;
        }

        public async Task<List<CinemaViewModel>> GetAllCinemasAsync()
        {
            List<CinemaViewModel> cinemas = new List<CinemaViewModel>();
            HttpResponseMessage response = await client.GetAsync(CinemasUrl);
            if (response.IsSuccessStatusCode)
            {
                cinemas = await response.Content.ReadAsAsync<List<CinemaViewModel>>();
            }
            return cinemas;
        }

        public async Task<CinemaViewModel> GetCinemaAsync(int id)
        {
            CinemaViewModel cinema = null;
            HttpResponseMessage response = await client.GetAsync(CinemasUrl + "?id=" + id);
            if (response.IsSuccessStatusCode)
            {
                cinema = await response.Content.ReadAsAsync<CinemaViewModel>();
            }
            return cinema;
        }

        public async Task CreateCinemaAsync(CinemaViewModel cinema)
        {
            HttpResponseMessage response = await client.PostAsJsonAsync(CinemasUrl, cinema);
            response.EnsureSuccessStatusCode();
        }

        public async Task UpdateCinemaAsync(CinemaViewModel cinema)
        {
            HttpResponseMessage response = await client.PutAsJsonAsync(CinemasUrl, cinema);
            response.EnsureSuccessStatusCode();
        }

        public async Task DeleteCinemaAsync(int id)
        {
            HttpResponseMessage response = await client.DeleteAsync(CinemasUrl + "?id=" + id);
            response.EnsureSuccessStatusCode();
        }
    }
}
